package com.gee.spring.boot.oauth2.auth.config.oauth2.token;

import com.gee.spring.boot.oauth2.common.constant.OauthConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * desc:
 *
 * @author gee
 * @date 2020/04/06 16:27
 */
@Configuration
public class TokenConfig {

    @Bean(value = "jwtTokenStore")
    public JwtTokenStore jwtTokenStore(){
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setAccessTokenConverter(geeAccessTokenConverter());
        converter.setSigningKey(OauthConstant.OAUTH2_JWT_SIGN_KEY);
        return converter;
    }

    private GeeAccessTokenConverter geeAccessTokenConverter(){
        GeeAccessTokenConverter accessTokenConverter = new GeeAccessTokenConverter();
        //如果要实现忘jwt的payload里添加信息, 要设置UserAuthenticationConverter的实现类
        GeeUserAuthenticationConverter userAuthenticationConverter = new GeeUserAuthenticationConverter();
        accessTokenConverter.setUserTokenConverter(userAuthenticationConverter);
        return accessTokenConverter;
    }
}
